package com.sikaryofficial.backend.manager.export;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sikaryofficial.backend.config.ActivityConfig;
import com.sikaryofficial.backend.constant.DownloadTaskTypeEnum;
import com.sikaryofficial.backend.domain.entity.DownloadTask;
import com.sikaryofficial.backend.domain.mapping.DownloadTaskMapping;
import com.sikaryofficial.backend.service.IDownloadTaskService;
import com.sikaryofficial.backend.utils.ProductUtil;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.system.api.RemoteUserDailyService;
import com.sikaryofficial.system.api.model.ManualExportReq;
import com.sikaryofficial.system.api.model.download.DownloadUrlDTO;
import com.sikaryofficial.system.api.model.user.UserDailyDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc : 活动导出
 * @date : 2023/10/30 15:39
 */
@Service
@Slf4j
@SuppressWarnings(value = "unchecked")
public class ActivityDailyExportManager extends AbstractExportManager {
    @Autowired
    private IDownloadTaskService downloadTaskService;
    @Autowired
    private ActivityConfig activityConfig;
    @Autowired
    private RemoteUserDailyService remoteUserDailyService;

    /**
     * 获取文件下载地址 （获奖名单）
     *
     * @return 文件下载地址对象
     */
    public List<DownloadUrlDTO> queryDownloadFilePath() {
        // 根据主键查询数据
        List<DownloadTask> downloadTaskList = downloadTaskService.getLatestTaskList(DownloadTaskTypeEnum.ACTIVITY_DAILY.getCode()
        );
        if (CollUtil.isEmpty(downloadTaskList)) {
            return Collections.emptyList();
        }
        return DownloadTaskMapping.INSTANCE.coverToUrlDTOList(downloadTaskList);
    }

    @Override
    public Integer getTaskTypeCode() {
        return DownloadTaskTypeEnum.ACTIVITY_DAILY.getCode();
    }

    @Override
    public String getTaskFolderPath(Long taskId) {
        return MessageFormat.format(activityConfig.getWinnerFolderPath(), taskId.toString());
    }

    @Override
    public void genFile(ManualExportReq req, Long activityId, File folder) {
        long startTime = System.currentTimeMillis();
        log.info("生成日活数据文件开始, activityId: {}", activityId);
        try {
            R<List<UserDailyDTO>> listR = remoteUserDailyService.queryDailyByTime(req);
            if (R.isError(listR) || Objects.isNull(listR.getData())) {
                log.error("日活数据为空，不生成文件, activityId: {}", activityId);
                return;
            }
            List<UserDailyDTO> userDailyDTOS = listR.getData();
            String fileName = IdWorker.getIdStr() + "." + activityConfig.getCsvFileType();
            String filePath = folder.getPath() + File.separator + fileName;
            writerTwo(userDailyDTOS, filePath);
        } catch (IOException e) {
            log.error("生成日活数据Excel文件失败, activityId: {}, 错误信息: {}", activityId, e.getMessage(), e);
        }
        log.info("生成日活数据文件结束, activityId: {},  耗时: {} ms", activityId, System.currentTimeMillis() - startTime);
    }

    @Override
    public String genLocalZipFile(File folder, Long activityId, Long taskId) {
        return ProductUtil.genLocalZipFile(folder, activityId, taskId);
    }
}
